package com.erlang.springmvc.controller;

import com.erlang.springmvc.pojo.Customer;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description：Restful实例
 */
@RestController
@RequestMapping("restful")
public class RestfulController {

    /**
     * @Description 使用get方式访问/restful/customer
     */
    @RequestMapping(value = "customer",method = RequestMethod.GET)
    public List<Customer> findAll(){
        List list = new ArrayList();
        for (int i = 0; i < 5; i++) {
            Customer customer = new Customer();
            customer.setCustId(i);
            customer.setCustName("小王"+i);
            customer.setCustLevel("普通客户"+i);
            customer.setCustPhone("15156400000"+i);
            customer.setCustAddress("上海"+i);
            customer.setCreaterTime(new Date());
            customer.setCustIndustry("IT"+i);
            customer.setCustSource("熊猫"+i);
            list.add(customer);
        }
        return list;
    }

    /**
     * @Description 使用post方式访问/restful/customer
     */
    @RequestMapping(value = "customer",method = RequestMethod.POST)
    public Boolean addCustomer(@RequestBody Customer customer){
        System.out.println("添加的用户信息："+customer.toString());
        return  Boolean.TRUE;
    }

    /**
     * @Description 使用delete方式访问/restful/customer/1
     */
    @RequestMapping(value = "customer/{customerId}",method = RequestMethod.DELETE)
    public Boolean deleteCustomer(@PathVariable("customerId") String id){
        System.out.println("删除用户信息："+id);
        return Boolean.TRUE;
    }
    
    //        @RequestMapping(value = "customer/{id}",method = RequestMethod.PUT)
    @PutMapping(value = "customer/{id}")
    public Boolean putCustomer(@PathVariable("id")String id){
        System.out.println("全量更新");
        return Boolean.TRUE;
    }

    /**
     * @Description 使用PATCH方式访问/restful/customer/1
     */
    @RequestMapping(value = "customer/{customerId}",method = RequestMethod.PATCH)
    public Boolean patchCustomer(@PathVariable("customerId") String id){
        System.out.println("后续部分更新");
        return Boolean.TRUE;
    }

    /**
     * @Description 使用get方式访问/restful/customer/1
     */
    @RequestMapping(value = "customer/{customerId}",method = RequestMethod.GET)
    public Customer findCustomerById(@PathVariable("customerId") String id){
        Customer customer = new Customer();
        customer.setCustId(1);
        customer.setCustName("小王"+1);
        customer.setCustLevel("普通客户"+1);
        customer.setCustPhone("15156400000"+1);
        customer.setCustAddress("上海"+1);
        customer.setCreaterTime(new Date());
        customer.setCustIndustry("IT"+1);
        customer.setCustSource("熊猫"+1);
        return customer;
    }
}